package com.example.costs;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import base.DB;
import base.Transaction;
import base.User;
import viewpagerindicator.TabPageIndicator;
import viewpagerindicator.TestFragment;
import viewpagerindicator.TitlePageIndicator;

public class MainActivity extends FragmentActivity{
    private static String[] tabs_title;
    private static String[] tabs_content = new String[] { "outcome_layout", "income_layout" };
    public static DB db;
    public static String trType = "e";
    public ListView listView;
    public static User user;
    public final static String EXTRA_MESSAGE = "com.example.costs.type";
    private TabPageIndicator indicator;
    public static  MainActivity mainActivity;
    public static ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabs_title = new String[] {getResources().getString(R.string.outcome), getResources().getString(R.string.income) };
        mainActivity = this;
        FragmentPagerAdapter adapter = new MainPageAdapter(getSupportFragmentManager());

        final ViewPager pager = (ViewPager)findViewById(R.id.pager);
        pager.setAdapter(adapter);
        viewPager = pager;
        indicator = (TabPageIndicator)findViewById(R.id.indicator);
        indicator.setViewPager(pager);
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                Log.d("pos",String.valueOf(position));
                switch (position) {
                    case 0:
                        MainActivity.trType = "e";
                        break;
                    case 1:
                        MainActivity.trType = "i";
                        break;
                }

                Transaction.getAllTr(pager, mainActivity.getLayoutInflater(), trType);
            }
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }
            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

        db = new DB(this);
        db.open();

        user = new User(1);


    }

    public void addTransaction(View view) {
        // Создаем объект Intent для вызова новой Activity
        Intent intent = new Intent(this, AddTransActivity.class);
        // Получаем текстовое поле в текущей Activity
        TabPageIndicator tabPageIndicator = (TabPageIndicator) findViewById(R.id.indicator);
        // запуск activity
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        db.close();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public class MainPageAdapter  extends FragmentPagerAdapter {
        public MainPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            TestFragment fragment = TestFragment.newInstance(tabs_content[position]);
            return fragment;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabs_title[position];
        }

        @Override
        public int getCount() {
            return tabs_title.length;
        }

    }

}


